package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/2/16 10:55
 * @description
 */
public class Code82 {
    private List<Integer> l1 = new ArrayList<>();
    private List<Integer> l2 = new ArrayList<>();

    public ListNode deleteDuplicates(ListNode head) {
        setList(head);
        l1.removeAll(l2);
        if (l1.size() == 0) {
            return null;
        }
        ListNode listNode = new ListNode(l1.get(0));
        setNode(listNode, 1);
        return listNode;
    }

    private void setList(ListNode node) {
        if (node != null) {
            int val = node.val;
            if (l1.contains(val)) {
                l2.add(val);
            } else {
                l1.add(val);
            }
            setList(node.next);
        }
    }

    private void setNode(ListNode node, int index) {
        if (index < l1.size()) {
            node.next = new ListNode(l1.get(index));
            setNode(node.next, index + 1);
        }
    }
}
